package com.homegoods.resource.util;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.text.SpannableStringBuilder;

import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.mvp.EmptyItem;
import com.kedll.library.mvp.ErrorItem;
import com.kedll.library.mvp.FooterItem;
import com.kedll.library.mvp.HeaderItem;
import com.kedll.library.util.SpanUtil;

/**
 * @author QiQi
 * @ClassName: Utils
 * @date 2018/9/12
 * @description
 */
public class Utils {


    public static SpannableStringBuilder getPrice(String price) {
        int pointIndex = price.indexOf(".");
        if (pointIndex == -1) {
            price += ".00";
            pointIndex = price.indexOf(".");
        }

        return SpanUtil.create()
                .addSection("￥")
                .setRelSize("￥", 0.75f)
                .addSection(price)
                .setRelSize(0.75f, pointIndex + 1, price.length() + 1)
                .getSpanStrBuilder();
    }

    public static GridLayoutManager getGridLayoutManager(Context context, MultiTypeAdapter adapter, int spanCount) {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, spanCount);
        GridLayoutManager.SpanSizeLookup spanSizeLookup = gridLayoutManager.getSpanSizeLookup();

        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (adapter.getItem(position) instanceof HeaderItem
                        || adapter.getItem(position) instanceof EmptyItem
                        || adapter.getItem(position) instanceof FooterItem
                        || adapter.getItem(position) instanceof ErrorItem) {
                    return gridLayoutManager.getSpanCount();
                }

                if (spanSizeLookup != null) {
                    spanSizeLookup.getSpanSize(position);
                }
                return 1;
            }
        });

        return gridLayoutManager;
    }

}
